package android.explorer.activity;

import java.io.File;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.app.Activity;
import android.explorer.R;
import android.explorer.dialogUltility.ShowYesNoDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class FolderDelete  extends Activity implements Runnable{
	Thread thread;
	long i=0;
	boolean stop=false;
	TextView cur;
	String path;
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.folderdelete);
		path=getIntent().getExtras().getString("foldername");
		((TextView) findViewById(R.id.folderdeletelabelaction)).setText("Delete folder "+path);
		
		thread=new Thread(this);
		thread.start();
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void run() {
		delete(path);
		finish();
	}
	public void delete(String path){
		if(stop)return;
		System.out.println(path);
		File file=new File(path);
		try {
			for(String str:file.list()){
				File f=new File(path+"/"+str);
				if(f.isFile()){
					System.out.print("delete file "+path);
					if(!f.delete()){
						new ShowYesNoDialog(this,"File "+f.getAbsolutePath()+" can not delete.\nDo you want break this operation ?","Delete",new OnClickListener(){
							public void onClick(DialogInterface arg0, int arg1) {
								stop=true;
							}
						});
						System.out.println("--> Failed");
					}else{
						System.out.println(" --> OK");
					}
				}else delete(f.getAbsolutePath());
				if(stop)return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			System.out.print("delete file "+path);
			file.delete();
			System.out.println(" --> OK");
		} catch (Exception e) {
			System.out.println("--> Failed");
			e.printStackTrace();
		}
	}
	public void onStopButtonClick(View target) {
		stop=true;
		finish();
	}
}
